package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private MenuDao menuDao;


    @Override
    public User findUserByUsername(String username) {
        User user = userDao.findUserByUsername(username);
        if (user == null) {
            return null;
        }
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findRoleByUserId(userId);
        for (Role role : roles) {
            Set<Permission> permissions = permissionDao.findPermissionByRoleId(role.getId());
            role.setPermissions(permissions);
        }
        user.setRoles(roles);
        return user;
    }

    @Override
    //根据用户名查找对应的角色和角色对应的菜单
    public List findMenuByUsername(String username) {
        List<Map<String, Object>> menuList = new ArrayList<>();
        List<Menu> menus = new ArrayList<>();
        Integer id = userDao.findIdByUsername(username);
        List<Integer> ids = roleDao.findRoleIdByUserId(id);
        for (Integer roleId : ids) {
            List<Integer> menuIds = menuDao.findMenuIdByRoleId(roleId);
            for (Integer menuId : menuIds) {
                Menu menu = menuDao.findMenuById(menuId);
                menus.add(menu);
            }
        }


        for (Menu menu : menus) {
            List<Menu> children = menuDao.findChildrenById(menu.getId());
            menu.setChildren(children);
        }
        Collections.sort(menus, new Comparator<Menu>() {
            @Override
            public int compare(Menu o1, Menu o2) {
                return o1.getPriority() - o2.getPriority();
            }
        });
        for (Menu menu : menus) {
            if (menu.getChildren() != null && menu.getChildren().size() > 0) {
                Map map = new HashMap();
                map.put("path", menu.getPath());
                map.put("title", menu.getName());
                map.put("icon", menu.getIcon());
                List<Menu> children = menu.getChildren();
                Collections.sort(children, new Comparator<Menu>() {
                    @Override
                    public int compare(Menu o1, Menu o2) {
                        return o1.getPriority() - o2.getPriority();
                    }
                });
                List<Map<String, Object>> childrenList = new ArrayList<>();
                for (Menu childMenu : children) {
                    Map childrenMap = new HashMap();
                    childrenMap.put("path", childMenu.getPath());
                    childrenMap.put("title", childMenu.getName());
                    childrenMap.put("icon", childMenu.getIcon());
                    childrenMap.put("linkUrl", childMenu.getLinkUrl());
                    childrenList.add(childrenMap);
                }
                map.put("children", childrenList);
                menuList.add(map);
            }
        }
        return menuList;
    }
}
